<?php
 
	function smarty_block_orders($params, $content, &$smarty, &$repeat) {

		global $db, $config, $counter;

		$l = count($smarty->_tag_stack);
		if(empty($params['name'])) $params['name'] = "orders";

		if(empty($smarty->blockvars[$l])) {
		 $sqlstr = "select a.*,b.pic,b.pid,(select stocks  from {$config->db_prefix}stocks as c where c.shopid=a.shopid and c.productid=a.productid group by a.productid) as stocks from {$config->db_prefix}orders as a left join haiou_product_sku as b on a.productid=b.id left join {$config->db_prefix}products as f on a.productid=f.product_sku_id where 1 ";
			if (!empty($params['id'])) $sqlstr .= " and a.id = ".$db->qstr($params['id']);
			if (!empty($params['shopid'])) $sqlstr .= " and a.shopid = ".$db->qstr($params['shopid']);
			if (isset($params['keywords'])) $sqlstr .= " and b.pname like ". $db->qstr("%".$params['keywords']."%");
			if (!empty($params['sdate'])) $sqlstr .= " and a.thedate>=". strtotime($params['sdate']);
			if (!empty($params['edate'])) $sqlstr .= " and a.thedate<=". (strtotime($params['edate'])+86399);
			if (!empty($params['lnumber'])) $sqlstr .= " and a.number>=". $db->qstr($params['lnumber']);
			if (!empty($params['hnumber'])) $sqlstr .= " and a.number<=". $db->qstr($params['hnumber']);
			if (!empty($params['lotnumber'])) $sqlstr .= " and a.lotnumber like ". $db->qstr("%".$params['lotnumber']."%");
			if (!empty($params['barcode'])) $sqlstr .= " and f.barcode=".$db->qstr($params['barcode']);
			if (!empty($params['product_id'])) $sqlstr .= " and b.pid in(".$params['product_id'].")";
			
			if (!empty($params['cond'])) $sqlstr .= " and ({$params['cond']})";
			
			if($params['type']!==null&&$params['type']!==''){
			    $sqlstr .= " and a.type= {$params['type']}";
			}
			if($params['status']==''){
				$params['status'] = '0';
			}
			$idsort = "DESC";
			if($params['status']==="0"){
			    $sqlstr .= " and a.timestocked= 0";
			    $idsort = "ASC";
			}elseif ($params['status']==="1"){
			    $sqlstr .= " and a.timestocked> 0";
			}elseif ($params['status']==="-1"){
			    $sqlstr .= " and a.timestocked= -1";
			}

			if (!empty($params['counter'])) $counter = $db->getrow(str_replace("a.*", "sum(a.number) number", $sqlstr));
			if (!empty($params['order'])) $sqlstr .= " order by a.thedate {$idsort},(IF(a.number-stocks>0,0,1)) DESC";
			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?(empty($_GET['list_page'])?1:$_GET['list_page']):$params['page'];
			$pager = new pager($limit, $page);
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);

			//
			if(!$smarty->blockvars[$l]) return $repeat = false;
			foreach ($smarty->blockvars[$l] as $kk=>$vv)
			{
			    $order_pc=$db->GetRow("select pic as product_pic  from haiou_order_product where product_id={$vv['productid']} and order_id={$vv['lotnumber']} limit 1");
			    if ($order_pc){
			     $smarty->blockvars[$l][$kk]['product_pic']=$order_pc['product_pic'];
			     continue;
			    }
				if(!($smarty->blockvars[$l][$kk]['product_pic'] = @strstr($vv['pic'], ",",true)))
				{
					$smarty->blockvars[$l][$kk]['product_pic'] = $vv['pic'];
				} 
				
			}
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}
	
		if(list($key, $item) = each($smarty->blockvars[$l])) {
		    $item['type']=$config->orders_type[$item['type']];
			$repeat = true;
			$pdb = new pager(9999, 1);
			if(!empty($counter)) $item["counter"] = $counter;
				
			$purchases=$pdb->execute($db,"select sum(number) as purchases_nums  from {$config->db_prefix}purchases where shopid={$item['shopid']} and productid={$item['productid']} and timestocked=0 and type=0");
			
			$item['purchases']=$purchases[0]['purchases_nums'];
			
			
			$smarty->assign($params['name'], $item);
		}
		else {
			$repeat = false;
			$counter = "";
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>